import {
    promisic
} from "../utils/util.js"
import {
    Http
} from "../utils/http.js"
import {
    Text
} from "./user1.js"
class Login {
    token
    pid
    userInfo
    newUser = false

    constructor(pid) {
        this.pid = pid || 0
        this._init()
        console.log("init once")
    }

    _init() {
        this.token = "123"
        this.temp()
    }

   async temp() {
        this._getLocalToken()
       this.token ? this._checkLogin() : this._wxLogin()
        this.token ? this._checkLogin() : this._wxLogin()
    }

    async _checkLogin() {
        console.log(211)
        const res = await Text.checkLogin(this.token)
       
        // const res = await Http.request({
        //     url: "/member/check_login",
        //     data: {
        //         token: this.token
        //     }
        // })
        this.userInfo = res.data
        // this.userInfo = {
        //     temp: 1
        // }
        this._upStorage()
    }


    async _wxLogin() {
        const res = await promisic(wx.login)()
        this.code = res.code
        this._tradeToken()
    }

    async _tradeToken() {
        const res = await Http.request({
            url: `/member/user_login`,
            data: {
                code: this.code,
                pid: this.pid
            }
        })
        this.token = res.data
        if (this.token) {
            this._checkLogin()
            this._upStorage()
        }
    }

    _getLocalToken() {
        this.token = wx.getStorageSync('token')
    }


    _upStorage() {
        wx.setStorageSync("token", this.token)
        wx.setStorageSync("userInfo", this.userInfo)
    }
}

export {
    Login
}